package com.qianbei.user.invitationCode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.qianbei.R;
import com.qianbei.common.base.BaseActivity;
import com.qianbei.common.init.SetData;
import com.qianbei.common.net.control.ServerControl;
import com.qianbei.common.net.control.ServerResult;
import com.qianbei.common.net.view.MsgAlert;
import com.qianbei.common.net.view.ProgressDialog;
import com.qianbei.common.rxbus.RxBus;
import com.qianbei.share.ui.ShareFragmentDialog;
import com.qianbei.user.ExampleActivity;
import com.qianbei.user.ShareUrlBean;
import com.umeng.socialize.UMShareAPI;

import java.util.List;

import static com.qianbei.common.init.SetData.getCareer;
import static com.qianbei.common.init.SetData.getFace;

public class InvitationCodeActivity extends BaseActivity implements View.OnClickListener, ShareFragmentDialog.OnShareSuccess {

    private EditText mEtInviationCode;
    private Button mBtInviationCode;
    private Button mBtInviationShare;
    private LinearLayout mLlInviationCode;
    private LinearLayout mLlNoEmptyView;
    private ImageView mIvNoNetWork;
    private String urlshare;
    private List<String> from;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_invitation_code);
        setTitle("输入邀请码");
        initView();
    }

    public void initView() {
        mEtInviationCode = (EditText) findViewById(R.id.et_inviation_code);
        mBtInviationCode = (Button) findViewById(R.id.bt_inviation_code);
        mBtInviationShare = (Button) findViewById(R.id.bt_inviation_share);
        mLlInviationCode = (LinearLayout) findViewById(R.id.ll_inviation_code);
        mLlNoEmptyView = (LinearLayout) findViewById(R.id.ll_no_emptyview);
        mIvNoNetWork = (ImageView) findViewById(R.id.iv_no_network);

        mBtInviationCode.setOnClickListener(this);
        mBtInviationShare.setOnClickListener(this);
        mIvNoNetWork.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_no_network:
//                if (ExampleUtil.isConnected(mContext)) {
//                    mLlInviationCode.setVisibility(View.VISIBLE);
//                    mLlNoEmptyView.setVisibility(View.GONE);
//                }
                break;
            case R.id.bt_inviation_code:
                String strCode = mEtInviationCode.getText().toString();
                if (!TextUtils.isEmpty(strCode)) {
                    ServerControl sc = new ServerControl(Request.Method.POST, INVIATE_CODE,
                            "uid", SetData.getUserID(),
                            "invicode", strCode);
                    sc.serverListener = new ServerControl.ServerListener() {
                        @Override
                        public void serverFinish(ServerResult sr) {
                            if (sr.isContinue) {

                                new MsgAlert().show(sr.bodyData.optString("msg"));
                                if (sr.bodyData.optString("status").equals("1")) {
                                    RxBus.getDefault().post("userinfo");
                                    Intent intent = new Intent();
                                    setResult(111, intent);
                                    finish();
                                }
                            }
                        }
                    };
                    new ProgressDialog(mContext).startControl(sc, "正在请求...");

                } else {
                    new MsgAlert().show("邀请码不能为空");
                }

                break;
            case R.id.bt_inviation_share:
                ServerControl sc = new ServerControl(Request.Method.GET, SHARE_WEBURL + SetData.getUserID() + "?");

                sc.serverListener = new ServerControl.ServerListener() {
                    @Override
                    public void serverFinish(ServerResult sr) {
                        if (sr.isContinue) {
                            ShareUrlBean shareUrlBean = JSON.parseObject(sr.bodyData.toString(), ShareUrlBean.class);

                            urlshare = shareUrlBean.getData().getUrl();
                            from = shareUrlBean.getData().getFrom();

                            if (urlshare == null || TextUtils.isEmpty(urlshare)) {
                                new MsgAlert().show("未获取到链接，请检查网络。");
                            } else {
                                ShareFragmentDialog shareFragmentDialog = ShareFragmentDialog.newInstance("快来和我一起赚零钱吧",
                                        "阅读创造价值，刷不完的有趣咨询，赚不完的零钱，快戳这里！",
                                        urlshare, "", "web", false);
                                shareFragmentDialog.show(getFragmentManager(), this.getClass().toString());
                                shareFragmentDialog.setOnShareSuccess(InvitationCodeActivity.this);
                            }
                        }
                    }
                };
                sc.startVolley();

                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        UMShareAPI.get(this).onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public boolean OnShareSuccess(boolean isShare) {
        if (isShare) {
            ServerControl sc = new ServerControl(Request.Method.GET,
                    SHARE_FRIENDS + SetData.getUserID() + "?");

            sc.serverListener = new ServerControl.ServerListener() {
                @Override
                public void serverFinish(ServerResult sr) {
                    if (sr.isContinue) {
                        //无提示
                    }
                }
            };
            sc.startVolley();
        }
        return false;
    }
}
